home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1995 June / MacFormat 25.iso / Shareware City / Developers / OutOfPhase1.1 Source / OutOfPhase Folder / ExportRAWSample.c < prev    next >
Text File  |  1994-08-24  |  24KB  |  678 lines

  1. /* ExportRAWSample.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "ExportRAWSample.h"
  31. #include "Screen.h"
  32. #include "RadioButton.h"
  33. #include "SimpleButton.h"
  34. #include "Memory.h"
  35. #include "Alert.h"
  36. #include "EventLoop.h"
  37. #include "Files.h"
  38. #include "BufferedFileOutput.h"
  39. #include "SampleStorageActual.h"
  40. #include "SampleConsts.h"
  41.  
  42.  
  43. typedef enum
  44.     {
  45.         eSignSigned EXECUTE(= -4523),
  46.         eSignUnsigned,
  47.         eSignSignBit
  48.     } SignModes;
  49.  
  50.  
  51. typedef enum
  52.     {
  53.         eEndianLittle EXECUTE(= -26123),
  54.         eEndianBig
  55.     } EndianType;
  56.  
  57.  
  58. #define WINDOWXSIZE (400)
  59.  
  60. #define PROMPTX (10)
  61. #define PROMPTY (5)
  62.  
  63. #define SIGNPROMPTX (PROMPTX)
  64. #define SIGNPROMPTY (PROMPTY + 20 + 6)
  65.  
  66. #define SIGNEDBUTTONX (SIGNPROMPTX + 100)
  67. #define SIGNEDBUTTONY (SIGNPROMPTY - 4)
  68. #define SIGNEDBUTTONWIDTH (80)
  69. #define SIGNEDBUTTONHEIGHT (21)
  70.  
  71. #define UNSIGNEDBUTTONX (SIGNEDBUTTONX + SIGNEDBUTTONWIDTH + 10)
  72. #define UNSIGNEDBUTTONY (SIGNEDBUTTONY)
  73. #define UNSIGNEDBUTTONWIDTH (SIGNEDBUTTONWIDTH)
  74. #define UNSIGNEDBUTTONHEIGHT (SIGNEDBUTTONHEIGHT)
  75.  
  76. #define SIGNBITBUTTONX (UNSIGNEDBUTTONX + UNSIGNEDBUTTONWIDTH + 10)
  77. #define SIGNBITBUTTONY (UNSIGNEDBUTTONY)
  78. #define SIGNBITBUTTONWIDTH (SIGNEDBUTTONWIDTH)
  79. #define SIGNBITBUTTONHEIGHT (SIGNEDBUTTONHEIGHT)
  80.  
  81. #define ENDIANPROMPTX (SIGNPROMPTX)
  82. #define ENDIANPROMPTY (SIGNPROMPTY + SIGNEDBUTTONHEIGHT + 3)
  83.  
  84. #define LITTLEENDIANBUTTONX (SIGNEDBUTTONX)
  85. #define LITTLEENDIANBUTTONY (ENDIANPROMPTY - 4)
  86. #define LITTLEENDIANBUTTONWIDTH (SIGNEDBUTTONWIDTH)
  87. #define LITTLEENDIANBUTTONHEIGHT (SIGNEDBUTTONHEIGHT)
  88.  
  89. #define BIGENDIANBUTTONX (LITTLEENDIANBUTTONX + LITTLEENDIANBUTTONWIDTH + 10)
  90. #define BIGENDIANBUTTONY (LITTLEENDIANBUTTONY)
  91. #define BIGENDIANBUTTONWIDTH (SIGNEDBUTTONWIDTH)
  92. #define BIGENDIANBUTTONHEIGHT (SIGNEDBUTTONHEIGHT)
  93.  
  94. #define OKBUTTONWIDTH (80)
  95. #define OKBUTTONHEIGHT (21)
  96. #define OKBUTTONX (WINDOWXSIZE / 4 - OKBUTTONWIDTH / 2)
  97. #define OKBUTTONY (BIGENDIANBUTTONY + BIGENDIANBUTTONHEIGHT + 13)
  98.  
  99. #define CANCELBUTTONWIDTH (OKBUTTONWIDTH)
  100. #define CANCELBUTTONHEIGHT (OKBUTTONHEIGHT)
  101. #define CANCELBUTTONX (3 * WINDOWXSIZE / 4 - OKBUTTONWIDTH / 2)
  102. #define CANCELBUTTONY (OKBUTTONY)
  103.  
  104. #define WINDOWYSIZE (CANCELBUTTONY + CANCELBUTTONHEIGHT + 10)
  105.  
  106.  
  107. typedef struct
  108.     {
  109.         WinType*                    ScreenID;
  110.         RadioButtonRec*        SignedButton;
  111.         RadioButtonRec*        UnsignedButton;
  112.         RadioButtonRec*        SignBitButton;
  113.         RadioButtonRec*        LittleEndian;
  114.         RadioButtonRec*        BigEndian;
  115.         SimpleButtonRec*    OKButton;
  116.         SimpleButtonRec*    CancelButton;
  117.     } WindowRec;
  118.  
  119.  
  120. static void                        RedrawWindow(WindowRec* Window)
  121.     {
  122.         CheckPtrExistence(Window);
  123.         RedrawRadioButton(Window->SignedButton);
  124.         RedrawRadioButton(Window->UnsignedButton);
  125.         RedrawRadioButton(Window->SignBitButton);
  126.         RedrawRadioButton(Window->LittleEndian);
  127.         RedrawRadioButton(Window->BigEndian);
  128.         RedrawSimpleButton(Window->OKButton);
  129.         RedrawSimpleButton(Window->CancelButton);
  130.         SetClipRect(Window->ScreenID,0,0,WINDOWXSIZE,WINDOWYSIZE);
  131.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Export Raw Sample:",18,
  132.             PROMPTX,PROMPTY,ePlain);
  133.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Sample Sign:",12,
  134.             SIGNPROMPTX,SIGNPROMPTY,ePlain);
  135.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Endianness:",11,
  136.             ENDIANPROMPTX,ENDIANPROMPTY,ePlain);
  137.     }
  138.  
  139.  
  140. /* this routine saves the data in the provided sample storage object as a raw */
  141. /* data file.  it handles any error reporting to the user.  the object is */
  142. /* NOT disposed, so the caller has to do that. */
  143. void                                ExportRAWSample(struct SampleStorageActualRec* TheSample)
  144.     {
  145.         WindowRec*                    Window;
  146.         MyBoolean                        LoopFlag;
  147.         MyBoolean                        DoItFlag EXECUTE(= -1324);
  148.  
  149.         CheckPtrExistence(TheSample);
  150.  
  151.         Window = (WindowRec*)AllocPtrCanFail(sizeof(WindowRec),"ExportRAWSample:  WindowRec");
  152.         if (Window == NIL)
  153.             {
  154.              FailurePoint1:
  155.                 AlertHalt("There is not enough memory available to export the sample.",NIL);
  156.                 return;
  157.             }
  158.  
  159.         Window->ScreenID = MakeNewWindow(eModelessDialogWindow,eWindowNotClosable,
  160.             eWindowNotZoomable,eWindowNotResizable,DialogLeftEdge(WINDOWXSIZE),
  161.             DialogTopEdge(WINDOWYSIZE),WINDOWXSIZE,WINDOWYSIZE,
  162.             (void (*)(void*))&RedrawWindow,Window);
  163.         if (Window->ScreenID == NIL)
  164.             {
  165.              FailurePoint2:
  166.                 ReleasePtr((char*)Window);
  167.                 goto FailurePoint1;
  168.             }
  169.         SetWindowName(Window->ScreenID,"Export Raw Sample");
  170.  
  171.         Window->SignedButton = NewRadioButton(Window->ScreenID,"Signed",
  172.             SIGNEDBUTTONX,SIGNEDBUTTONY,SIGNEDBUTTONWIDTH,SIGNEDBUTTONHEIGHT);
  173.         if (Window->SignedButton == NIL)
  174.             {
  175.              FailurePoint3:
  176.                 KillWindow(Window->ScreenID);
  177.                 goto FailurePoint2;
  178.             }
  179.         SetRadioButtonState(Window->SignedButton,True); /* signed by default */
  180.  
  181.         Window->UnsignedButton = NewRadioButton(Window->ScreenID,"Unsigned",
  182.             UNSIGNEDBUTTONX,UNSIGNEDBUTTONY,UNSIGNEDBUTTONWIDTH,UNSIGNEDBUTTONHEIGHT);
  183.         if (Window->UnsignedButton == NIL)
  184.             {
  185.              FailurePoint4:
  186.                 DisposeRadioButton(Window->SignedButton);
  187.                 goto FailurePoint3;
  188.             }
  189.  
  190.         Window->SignBitButton = NewRadioButton(Window->ScreenID,"Sign Bit",
  191.             SIGNBITBUTTONX,SIGNBITBUTTONY,SIGNBITBUTTONWIDTH,SIGNBITBUTTONHEIGHT);
  192.         if (Window->SignBitButton == NIL)
  193.             {
  194.              FailurePoint5:
  195.                 DisposeRadioButton(Window->UnsignedButton);
  196.                 goto FailurePoint4;
  197.             }
  198.  
  199.         Window->LittleEndian = NewRadioButton(Window->ScreenID,"Little Endian",
  200.             LITTLEENDIANBUTTONX,LITTLEENDIANBUTTONY,LITTLEENDIANBUTTONWIDTH,
  201.             LITTLEENDIANBUTTONHEIGHT);
  202.         if (Window->LittleEndian == NIL)
  203.             {
  204.              FailurePoint6:
  205.                 DisposeRadioButton(Window->SignBitButton);
  206.                 goto FailurePoint5;
  207.             }
  208.         SetRadioButtonState(Window->LittleEndian,True); /* little endian by default */
  209.  
  210.         Window->BigEndian = NewRadioButton(Window->ScreenID,"Big Endian",
  211.             BIGENDIANBUTTONX,BIGENDIANBUTTONY,BIGENDIANBUTTONWIDTH,BIGENDIANBUTTONHEIGHT);
  212.         if (Window->BigEndian == NIL)
  213.             {
  214.              FailurePoint7:
  215.                 DisposeRadioButton(Window->LittleEndian);
  216.                 goto FailurePoint6;
  217.             }
  218.  
  219.         Window->OKButton = NewSimpleButton(Window->ScreenID,"OK",OKBUTTONX,OKBUTTONY,
  220.             OKBUTTONWIDTH,OKBUTTONHEIGHT);
  221.         if (Window->OKButton == NIL)
  222.             {
  223.              FailurePoint8:
  224.                 DisposeRadioButton(Window->BigEndian);
  225.                 goto FailurePoint7;
  226.             }
  227.         SetDefaultButtonState(Window->OKButton,True);
  228.  
  229.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",CANCELBUTTONX,
  230.             CANCELBUTTONY,CANCELBUTTONWIDTH,CANCELBUTTONHEIGHT);
  231.         if (Window->CancelButton == NIL)
  232.             {
  233.              FailurePoint9:
  234.                 DisposeSimpleButton(Window->OKButton);
  235.                 goto FailurePoint8;
  236.             }
  237.  
  238.         LoopFlag = True;
  239.         while (LoopFlag)
  240.             {
  241.                 OrdType                            X;
  242.                 OrdType                            Y;
  243.                 ModifierFlags                Modifiers;
  244.                 struct MenuItemType*    MenuItem;
  245.                 char                                KeyPress;
  246.  
  247.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  248.                     {
  249.                         default:
  250.                             break;
  251.                         case eCheckCursor:
  252.                             SetArrowCursor();
  253.                             break;
  254.                         case eNoEvent:
  255.                             break;
  256.                         case eMenuStarting:
  257.                             break;
  258.                         case eMenuCommand:
  259.                             EXECUTE(PRERR(AllowResume,
  260.                                 "DoImportDialog: Undefined menu option chosen"));
  261.                             break;
  262.                         case eKeyPressed:
  263.                             if (KeyPress == 13)
  264.                                 {
  265.                                     FlashButton(Window->OKButton);
  266.                                     DoItFlag = True;
  267.                                     LoopFlag = False;
  268.                                 }
  269.                             else if (KeyPress == eCancelKey)
  270.                                 {
  271.                                     FlashButton(Window->CancelButton);
  272.                                     DoItFlag = False;
  273.                                     LoopFlag = False;
  274.                                 }
  275.                             break;
  276.                         case eMouseDown:
  277.                             if (SimpleButtonHitTest(Window->OKButton,X,Y))
  278.                                 {
  279.                                     if (SimpleButtonMouseDown(Window->OKButton,X,Y,NIL,NIL))
  280.                                         {
  281.                                             DoItFlag = True;
  282.                                             LoopFlag = False;
  283.                                         }
  284.                                 }
  285.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  286.                                 {
  287.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  288.                                         {
  289.                                             DoItFlag = False;
  290.                                             LoopFlag = False;
  291.                                         }
  292.                                 }
  293.                             else if (RadioButtonHitTest(Window->SignedButton,X,Y))
  294.                                 {
  295.                                     if (RadioButtonMouseDown(Window->SignedButton,X,Y))
  296.                                         {
  297.                                             SetRadioButtonState(Window->UnsignedButton,False);
  298.                                             SetRadioButtonState(Window->SignBitButton,False);
  299.                                         }
  300.                                 }
  301.                             else if (RadioButtonHitTest(Window->UnsignedButton,X,Y))
  302.                                 {
  303.                                     if (RadioButtonMouseDown(Window->UnsignedButton,X,Y))
  304.                                         {
  305.                                             SetRadioButtonState(Window->SignedButton,False);
  306.                                             SetRadioButtonState(Window->SignBitButton,False);
  307.                                         }
  308.                                 }
  309.                             else if (RadioButtonHitTest(Window->SignBitButton,X,Y))
  310.                                 {
  311.                                     if (RadioButtonMouseDown(Window->SignBitButton,X,Y))
  312.                                         {
  313.                                             SetRadioButtonState(Window->SignedButton,False);
  314.                                             SetRadioButtonState(Window->UnsignedButton,False);
  315.                                         }
  316.                                 }
  317.                             else if (RadioButtonHitTest(Window->LittleEndian,X,Y))
  318.                                 {
  319.                                     if (RadioButtonMouseDown(Window->LittleEndian,X,Y))
  320.                                         {
  321.                                             SetRadioButtonState(Window->BigEndian,False);
  322.                                         }
  323.                                 }
  324.                             else if (RadioButtonHitTest(Window->BigEndian,X,Y))
  325.                                 {
  326.                                     if (RadioButtonMouseDown(Window->BigEndian,X,Y))
  327.                                         {
  328.                                             SetRadioButtonState(Window->LittleEndian,False);
  329.                                         }
  330.                                 }
  331.                             break;
  332.                     }
  333.             }
  334.         ERROR((DoItFlag != True) && (DoItFlag != False),PRERR(ForceAbort,
  335.             "DoImportDialog:  DoItFlag is neither true nor false"));
  336.  
  337.         if (DoItFlag)
  338.             {
  339.                 FileSpec*                    WhereToPutIt;
  340.                 SignModes                    Signing;
  341.                 EndianType                Endianness;
  342.  
  343.                 if (GetRadioButtonState(Window->LittleEndian))
  344.                     {
  345.                         Endianness = eEndianLittle;
  346.                     }
  347.                 else if (GetRadioButtonState(Window->BigEndian))
  348.                     {
  349.                         Endianness = eEndianBig;
  350.                     }
  351.                 else
  352.                     {
  353.                         EXECUTE(PRERR(AllowResume,"ExportRAWSample:  bad endian buttons"));
  354.                     }
  355.  
  356.                 if (GetRadioButtonState(Window->SignedButton))
  357.                     {
  358.                         Signing = eSignSigned;
  359.                     }
  360.                 else if (GetRadioButtonState(Window->UnsignedButton))
  361.                     {
  362.                         Signing = eSignUnsigned;
  363.                     }
  364.                 else if (GetRadioButtonState(Window->SignBitButton))
  365.                     {
  366.                         Signing = eSignSignBit;
  367.                     }
  368.                 else
  369.                     {
  370.                         EXECUTE(PRERR(AllowResume,"ExportRAWSample:  bad signing buttons"));
  371.                     }
  372.  
  373.                 WhereToPutIt = PutFile("Untitled.RAW");
  374.                 if (WhereToPutIt != NIL)
  375.                     {
  376.                         if (CreateFile(WhereToPutIt,ApplicationCreator,CODE4BYTES('?','?','?','?')))
  377.                             {
  378.                                 FileType*                    FileDesc;
  379.  
  380.                                 if (OpenFile(WhereToPutIt,&FileDesc,eReadAndWrite))
  381.                                     {
  382.                                         BufferedOutputRec*    File;
  383.  
  384.                                         File = NewBufferedOutput(FileDesc);
  385.                                         if (File != NIL)
  386.                                             {
  387.                                                 long                                Scan;
  388.                                                 long                                Limit;
  389.  
  390.                                                 Limit = GetSampleStorageActualNumFrames(TheSample);
  391.                                                 switch (GetSampleStorageActualNumChannels(TheSample))
  392.                                                     {
  393.                                                         default:
  394.                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad num channels"));
  395.                                                             break;
  396.                                                         case eSampleStereo:
  397.                                                             switch (GetSampleStorageActualNumBits(TheSample))
  398.                                                                 {
  399.                                                                     default:
  400.                                                                         EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad number of bits"));
  401.                                                                         break;
  402.                                                                     case eSample8bit:
  403.                                                                         for (Scan = 0; Scan < Limit; Scan += 1)
  404.                                                                             {
  405.                                                                                 largefixedsigned        SampleLeft;
  406.                                                                                 largefixedsigned        SampleRight;
  407.                                                                                 unsigned char                DataLeft;
  408.                                                                                 unsigned char                DataRight;
  409.  
  410.                                                                                 SampleLeft = GetSampleStorageActualValue(
  411.                                                                                     TheSample,Scan,eLeftChannel);
  412.                                                                                 SampleRight = GetSampleStorageActualValue(
  413.                                                                                     TheSample,Scan,eRightChannel);
  414.                                                                                 switch (Signing)
  415.                                                                                     {
  416.                                                                                         default:
  417.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad signing selector"));
  418.                                                                                             break;
  419.                                                                                         case eSignSigned:
  420.                                                                                             DataLeft = roundtonearest(largefixed2double(SampleLeft)
  421.                                                                                                 * MAX8BIT);
  422.                                                                                             DataRight = roundtonearest(largefixed2double(SampleRight)
  423.                                                                                                 * MAX8BIT);
  424.                                                                                             break;
  425.                                                                                         case eSignUnsigned:
  426.                                                                                             DataLeft = roundtonearest(largefixed2double(SampleLeft)
  427.                                                                                                 * MAX8BIT) + 128;
  428.                                                                                             DataRight = roundtonearest(largefixed2double(SampleRight)
  429.                                                                                                 * MAX8BIT) + 128;
  430.                                                                                             break;
  431.                                                                                         case eSignSignBit:
  432.                                                                                             if (SampleLeft >= 0)
  433.                                                                                                 {
  434.                                                                                                     DataLeft = roundtonearest(largefixed2double(SampleLeft)
  435.                                                                                                         * MAX8BIT);
  436.                                                                                                 }
  437.                                                                                              else
  438.                                                                                                 {
  439.                                                                                                     DataLeft = roundtonearest(- largefixed2double(SampleLeft)
  440.                                                                                                         * MAX8BIT) | 0x80;
  441.                                                                                                 }
  442.                                                                                             if (SampleRight >= 0)
  443.                                                                                                 {
  444.                                                                                                     DataRight = roundtonearest(largefixed2double(SampleRight)
  445.                                                                                                         * MAX8BIT);
  446.                                                                                                 }
  447.                                                                                              else
  448.                                                                                                 {
  449.                                                                                                     DataRight = roundtonearest(- largefixed2double(SampleRight)
  450.                                                                                                         * MAX8BIT) | 0x80;
  451.                                                                                                 }
  452.                                                                                             break;
  453.                                                                                     }
  454.                                                                                 if (!WriteBufferedUnsignedChar(File,DataLeft))
  455.                                                                                     {
  456.                                                                                      DiskErrorPoint:
  457.                                                                                         AlertHalt("Unable to write the data to the file.",NIL);
  458.                                                                                         goto FailureEscapePoint;
  459.                                                                                     }
  460.                                                                                 if (!WriteBufferedUnsignedChar(File,DataLeft))
  461.                                                                                     {
  462.                                                                                         goto DiskErrorPoint;
  463.                                                                                     }
  464.                                                                             }
  465.                                                                         break;
  466.                                                                     case eSample16bit:
  467.                                                                         for (Scan = 0; Scan < Limit; Scan += 1)
  468.                                                                             {
  469.                                                                                 largefixedsigned        SampleLeft;
  470.                                                                                 largefixedsigned        SampleRight;
  471.                                                                                 unsigned short            DataLeft;
  472.                                                                                 unsigned short            DataRight;
  473.  
  474.                                                                                 SampleLeft = GetSampleStorageActualValue(
  475.                                                                                     TheSample,Scan,eLeftChannel);
  476.                                                                                 SampleRight = GetSampleStorageActualValue(
  477.                                                                                     TheSample,Scan,eRightChannel);
  478.                                                                                 switch (Signing)
  479.                                                                                     {
  480.                                                                                         default:
  481.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad signing selector"));
  482.                                                                                             break;
  483.                                                                                         case eSignSigned:
  484.                                                                                             DataLeft = roundtonearest(largefixed2double(SampleLeft)
  485.                                                                                                 * MAX16BIT);
  486.                                                                                             DataRight = roundtonearest(largefixed2double(SampleRight)
  487.                                                                                                 * MAX16BIT);
  488.                                                                                             break;
  489.                                                                                         case eSignUnsigned:
  490.                                                                                             DataLeft = roundtonearest(largefixed2double(SampleLeft)
  491.                                                                                                 * MAX16BIT) + 32768;
  492.                                                                                             DataRight = roundtonearest(largefixed2double(SampleRight)
  493.                                                                                                 * MAX16BIT) + 32768;
  494.                                                                                             break;
  495.                                                                                         case eSignSignBit:
  496.                                                                                             if (SampleLeft >= 0)
  497.                                                                                                 {
  498.                                                                                                     DataLeft = roundtonearest(largefixed2double(SampleLeft)
  499.                                                                                                         * MAX16BIT);
  500.                                                                                                 }
  501.                                                                                              else
  502.                                                                                                 {
  503.                                                                                                     DataLeft = roundtonearest(- largefixed2double(SampleLeft)
  504.                                                                                                         * MAX16BIT) | 0x8000;
  505.                                                                                                 }
  506.                                                                                             if (SampleRight >= 0)
  507.                                                                                                 {
  508.                                                                                                     DataRight = roundtonearest(largefixed2double(SampleRight)
  509.                                                                                                         * MAX16BIT);
  510.                                                                                                 }
  511.                                                                                              else
  512.                                                                                                 {
  513.                                                                                                     DataRight = roundtonearest(- largefixed2double(SampleRight)
  514.                                                                                                         * MAX16BIT) | 0x8000;
  515.                                                                                                 }
  516.                                                                                             break;
  517.                                                                                     }
  518.                                                                                 switch (Endianness)
  519.                                                                                     {
  520.                                                                                         default:
  521.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad endianness selector"));
  522.                                                                                             break;
  523.                                                                                         case eEndianLittle:
  524.                                                                                             if (!WriteBufferedUnsignedShortLittleEndian(File,DataLeft))
  525.                                                                                                 {
  526.                                                                                                     goto DiskErrorPoint;
  527.                                                                                                 }
  528.                                                                                             if (!WriteBufferedUnsignedShortLittleEndian(File,DataLeft))
  529.                                                                                                 {
  530.                                                                                                     goto DiskErrorPoint;
  531.                                                                                                 }
  532.                                                                                             break;
  533.                                                                                         case eEndianBig:
  534.                                                                                             if (!WriteBufferedUnsignedShortBigEndian(File,DataLeft))
  535.                                                                                                 {
  536.                                                                                                     goto DiskErrorPoint;
  537.                                                                                                 }
  538.                                                                                             if (!WriteBufferedUnsignedShortBigEndian(File,DataLeft))
  539.                                                                                                 {
  540.                                                                                                     goto DiskErrorPoint;
  541.                                                                                                 }
  542.                                                                                             break;
  543.                                                                                     }
  544.                                                                             }
  545.                                                                         break;
  546.                                                                 }
  547.                                                             break;
  548.                                                         case eSampleMono:
  549.                                                             switch (GetSampleStorageActualNumBits(TheSample))
  550.                                                                 {
  551.                                                                     default:
  552.                                                                         EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad number of bits"));
  553.                                                                         break;
  554.                                                                     case eSample8bit:
  555.                                                                         for (Scan = 0; Scan < Limit; Scan += 1)
  556.                                                                             {
  557.                                                                                 largefixedsigned        Sample;
  558.                                                                                 unsigned char                Data;
  559.  
  560.                                                                                 Sample = GetSampleStorageActualValue(
  561.                                                                                     TheSample,Scan,eMonoChannel);
  562.                                                                                 switch (Signing)
  563.                                                                                     {
  564.                                                                                         default:
  565.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad signing selector"));
  566.                                                                                             break;
  567.                                                                                         case eSignSigned:
  568.                                                                                             Data = roundtonearest(largefixed2double(Sample) * MAX8BIT);
  569.                                                                                             break;
  570.                                                                                         case eSignUnsigned:
  571.                                                                                             Data = roundtonearest(largefixed2double(Sample) * MAX8BIT) + 128;
  572.                                                                                             break;
  573.                                                                                         case eSignSignBit:
  574.                                                                                             if (Sample >= 0)
  575.                                                                                                 {
  576.                                                                                                     Data = roundtonearest(largefixed2double(Sample) * MAX8BIT);
  577.                                                                                                 }
  578.                                                                                              else
  579.                                                                                                 {
  580.                                                                                                     Data = roundtonearest(- largefixed2double(Sample)
  581.                                                                                                         * MAX8BIT) | 0x80;
  582.                                                                                                 }
  583.                                                                                             break;
  584.                                                                                     }
  585.                                                                                 if (!WriteBufferedUnsignedChar(File,Data))
  586.                                                                                     {
  587.                                                                                         goto DiskErrorPoint;
  588.                                                                                     }
  589.                                                                             }
  590.                                                                         break;
  591.                                                                     case eSample16bit:
  592.                                                                         for (Scan = 0; Scan < Limit; Scan += 1)
  593.                                                                             {
  594.                                                                                 largefixedsigned        Sample;
  595.                                                                                 unsigned short            Data;
  596.  
  597.                                                                                 Sample = GetSampleStorageActualValue(
  598.                                                                                     TheSample,Scan,eMonoChannel);
  599.                                                                                 switch (Signing)
  600.                                                                                     {
  601.                                                                                         default:
  602.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad signing selector"));
  603.                                                                                             break;
  604.                                                                                         case eSignSigned:
  605.                                                                                             Data = roundtonearest(largefixed2double(Sample) * MAX16BIT);
  606.                                                                                             break;
  607.                                                                                         case eSignUnsigned:
  608.                                                                                             Data = roundtonearest(largefixed2double(Sample) * MAX16BIT) + 32768;
  609.                                                                                             break;
  610.                                                                                         case eSignSignBit:
  611.                                                                                             if (Sample >= 0)
  612.                                                                                                 {
  613.                                                                                                     Data = roundtonearest(largefixed2double(Sample) * MAX16BIT);
  614.                                                                                                 }
  615.                                                                                              else
  616.                                                                                                 {
  617.                                                                                                     Data = roundtonearest(- largefixed2double(Sample)
  618.                                                                                                         * MAX16BIT) | 0x8000;
  619.                                                                                                 }
  620.                                                                                             break;
  621.                                                                                     }
  622.                                                                                 switch (Endianness)
  623.                                                                                     {
  624.                                                                                         default:
  625.                                                                                             EXECUTE(PRERR(ForceAbort,"ExportRAWSample:  bad endianness selector"));
  626.                                                                                             break;
  627.                                                                                         case eEndianLittle:
  628.                                                                                             if (!WriteBufferedUnsignedShortLittleEndian(File,Data))
  629.                                                                                                 {
  630.                                                                                                     goto DiskErrorPoint;
  631.                                                                                                 }
  632.                                                                                             break;
  633.                                                                                         case eEndianBig:
  634.                                                                                             if (!WriteBufferedUnsignedShortBigEndian(File,Data))
  635.                                                                                                 {
  636.                                                                                                     goto DiskErrorPoint;
  637.                                                                                                 }
  638.                                                                                             break;
  639.                                                                                     }
  640.                                                                             }
  641.                                                                         break;
  642.                                                                 }
  643.                                                             break;
  644.                                                     }
  645.                                              FailureEscapePoint:
  646.                                                 EndBufferedOutput(File);
  647.                                             }
  648.                                          else
  649.                                             {
  650.                                                 AlertHalt("There is not enough memory available to "
  651.                                                     "export the sample.",NIL);
  652.                                             }
  653.                                         CloseFile(FileDesc);
  654.                                     }
  655.                                  else
  656.                                     {
  657.                                         AlertHalt("The file could not be opened for writing.",NIL);
  658.                                     }
  659.                             }
  660.                          else
  661.                             {
  662.                                 AlertHalt("The file could not be created.",NIL);
  663.                             }
  664.                         DisposeFileSpec(WhereToPutIt);
  665.                     }
  666.             }
  667.  
  668.         DisposeRadioButton(Window->SignedButton);
  669.         DisposeRadioButton(Window->UnsignedButton);
  670.         DisposeRadioButton(Window->SignBitButton);
  671.         DisposeRadioButton(Window->LittleEndian);
  672.         DisposeRadioButton(Window->BigEndian);
  673.         DisposeSimpleButton(Window->OKButton);
  674.         DisposeSimpleButton(Window->CancelButton);
  675.         KillWindow(Window->ScreenID);
  676.         ReleasePtr((char*)Window);
  677.     }
  678.